package com.huonan.jb.tools.fileuse.compare;

import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.huonan.utils.FileUtils;
import com.huonan.utils.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

/**
 * @Description 根据指定目录 和遍历深度遍历出所有文件
 **/
@Slf4j
public class FileHandler {
    /**
     * 需要比较的字段
     */
    private static final String COMPARE_FIELD = "kw_url";
    public static final String DATA_TYPE = "data";
    private static final Map<String, Integer> statistics_map = new HashMap<>();

    /**
     * @return java.util.List<java.io.File>
     * @Description 获取指定规则目录下的所有问阿金
     * @Param [sourcePath, depth]
     **/
    @NotNull
    public static List<File> traverseAllFiles(String sourcePath, int depth) {
        //过滤文件
        FileFilter fileFilter = pathname -> {
            String fileName = pathname.getName();
            return fileName.startsWith(DATA_TYPE) && !fileName.endsWith(".DS_Store") && !fileName.endsWith(".zip");
        };
        return FileUtil.loopFiles(Paths.get(sourcePath), depth, fileFilter);
    }

    /**
     * @return java.util.List<java.lang.String> 返回筛选成功的数据
     * @Description @Description 根据比较内容 筛选源字段内容
     * @Param [kwIdSet, list] 比较内容，源字段内容
     **/
    @NotNull
    public static List<String> filterFields(List<String> kwIdSet, List<String> list) {
        return list.stream()
                .filter(line -> kwIdSet.contains(JSONUtil.parseObj(line).getStr(COMPARE_FIELD)))
                .collect(Collectors.toList());
    }

    public static void writeToFile(String OUTDIR, File file, List<String> hashKwId) {
        StringJoiner hashIdSj = new StringJoiner("\n");
        //文件父目录
        String filePath = file.getParent();
        String parentDirName = filePath.substring(filePath.lastIndexOf("/") + 1);
        hashKwId.forEach(hashIdSj::add);
        File outFile = new File(OUTDIR + "/filtered/" + parentDirName + "/filter_" + file.getName());
        log.info("输出文件: {}", outFile.getPath());

    }

    public static void compareContent(String outDir, TimeInterval timer, ExecutorService executorService,
            List<String> kwIdSet, File file) {
        executorService.execute(() -> {
            //文件内容
            //List<String> list = readOnce(outDir, kwIdSet, file);
            int readCount = readAsStream(outDir, file.getPath(), kwIdSet);
            log.info("文件：{},数据量：{},总耗时：{}",
                    file.getPath(), readCount, timer.interval());
        });
    }

    public static void compareContent2(String outDir, TimeInterval timer, ExecutorService executorService,
                                      List<String> kwIdSet, File file) {
        CompletableFuture.supplyAsync(()->{
            int readCount = readAsStream(outDir, file.getPath(), kwIdSet);
            log.info("文件：{},数据量：{},总耗时：{}",
                    file.getPath(), readCount, timer.interval());
            return null;
        },executorService);
    }

    /*一次读取文件*/
    public static List<String> extractJsonData(List<String> lines) {
        List<String> list = new ArrayList<>();
        for (String line : lines) {
            JSONObject obj = JSONUtil.parseObj(line);
            String[] businessIndustry = obj.get("kw_businessIndustry", String[].class);
            String[] cooperateBinds = obj.get("kw_cooperateBinds", String[].class);
            if (cooperateBinds != null) {
                Map<String, Object> map = new HashMap<>();
                map.put("kw_businessIndustry", businessIndustry);
                map.put("kw_cooperateBinds", cooperateBinds);
                try {
                    JSONObject obj1 = JSONUtil.createObj();
                    String toJson = JacksonUtil.convertMapToJson(map);
                    list.add(toJson);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return list;
    }

    /*流式读取文件*/
    public static int readAsStream(String outDir, String cmpFile, List<String> kwIdSet) {
        int readCount = 0;
        log.info("开始读文件:{}", outDir);
        FileUtils.writeAppend(new File(outDir), "文件:" + cmpFile, "UTF-8");
        try (BufferedReader reader = new BufferedReader(new FileReader(cmpFile))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String uid = JSONUtil.parseObj(line).getStr(COMPARE_FIELD);
                if (uid.contains(kwIdSet.get(0))) {
                    readCount++;
                    FileUtils.writeAppend(new File(outDir), line, "UTF-8");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (readCount == 0) {
            FileUtils.writeAppend(new File(outDir), "0", "UTF-8");
        }
        return readCount;
    }
}
